package com.cg.design.graphicdesignpattern.decorator;

/**
 * 表示用字符串装饰上下两边框的类，是Border的子类
 *
 * @program: LeetCode->UpDownBorder
 * @description: 表示用字符串装饰上下两边框的类，是Border的子类
 * @author: cg
 * @create: 2022-03-25 19:43
 **/
public class UpDownBorder extends Border {

    /**
     * 表示装饰边框额字符
     */
    private final char borderChar;

    public UpDownBorder(Display display, char borderChar) {
        super(display);
        this.borderChar = borderChar;
    }

    @Override
    public int getColumns() {
        // 字符数要与显示的内容的字符数相同
        return display.getColumns();
    }

    @Override
    public int getRows() {
        // 行数是内容的行数加上上下边框
        return 1 + display.getRows() + 1;
    }

    @Override
    public String getRowText(int row) {
        if (row == 0 || row == getRows() - 1) {
            return makeLine(borderChar, getColumns());
        } else {
            return display.getRowText(row - 1);
        }
    }

    /**
     * 生成一个由count个字符ch连续组成的字符串
     *
     * @param ch
     * @param count
     * @return
     */
    private String makeLine(char ch, int count) {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < count; i++) {
            buffer.append(ch);
        }
        return buffer.toString();
    }

}
